%
%------------------------------------------------------
% real valued genetic algorithm 
% for the minimization of the rastrigin function 
% (with dynamic display for the case of 2 parameters)
%
%-----------------------------------------------------
% selection: proportionate with ranking method 
% crossover: blend 
% mutation: non uniform
% 1-elitism
%

%
%---------main program ---------------------
%


disp('real valued GA for minimizing the rastrigin function');
disp('(with dynamic display for 2 parameters)');
%
xmin=-5.12;xmax=5.12;N=300;
x=xmin:((xmax-xmin)/(N-1)):xmax;
y=x;
z=zeros(N,N);
for i=1:N
for j=1:N
z(i,j)=rastrigin([x(i),y(j)],2);
end
end
%
n=2;%evstr(x_dialog('parameter number of the rastrigin function','2')); 
Npop=30;%evstr(x_dialog('population number','30'));     
Ngen=50;%evstr(x_dialog('generation number','50')); 
pc=0.9;%evstr(x_dialog('crossover probability','0.9')); 
pm=0.6;%evstr(x_dialog('mutation probability','0.6')); 
%
xmin=-5.12*ones(1,n);
xmax=5.12*ones(1,n);
Xmin=ones(Npop,1)*xmin;
Xmax=ones(Npop,1)*xmax;
u=rand(Npop,n);
pop=Xmin+(Xmax-Xmin).*u;  % random initialisation of the population
Xpar=[pop,zeros(Npop,1)];  
Xoff=Xpar;Xoldpar=Xpar;fmin=[];mineval=0;newval=zeros(Npop,1);Traj=[];
%
 %
for gen=1:Ngen;
  Xpar=Xoff;
  [Xpar,bestpar,bestnewpop]=evalplus1elitism(Xpar,Xoldpar,Npop,gen,n);  % evaluation plus 1-elitism
  Xoldpar=Xpar;
  fmin=[fmin,bestpar(numel(bestnewpop))];
  %
  Xpar=rankselect(Xpar); % rank selection
  for i=1:2:(Npop-1)
    u1=1+int32((Npop-1)*rand());u2=1+int32((Npop-1)*rand());u3=int32(rand());
    xpar=[Xpar(u1,:);Xpar(u2,:)];
    Xoff(i:(i+1),:)=xpar;
    if (u3<pc)
     xoff=blcross(xpar);   %crossover
     Xoff(i:(i+1),:)=xoff;
    end
    for j=1:2
       u4=rand();
        if (u4<pm)
          xoffl=mutate(xoff(j,:),gen,Ngen,xmin,xmax);  % mutation
          Xoff(i+j-1,:)=xoffl;
        end
    end
   end
Traj=[Traj;bestpar(1),bestpar(2)];
%
end


h1=figure('Visible','off','IntegerHandle','Off');
hold on;
%Need to alter the camer position so
%hget the child property of the figure which are the axes
hax=get(h1,'Children');
%set(hax,'CameraPosition',[-24 -11 5]);
xlabel('x')
ylabel('y')
title('Contour plot of rastrigin function')
colorbar('visible')
contour(x,y,z);
%print the most recent figure to a file
print -djpeg 'gacontour.jpg'
hold off;



%plot(Xpar(:,1),Xpar(:,2));
%plot(Traj(:,1),Traj(:,2));

[Xpar,bestpar,bestnewpop]=evalplus1elitism(Xoff,Xoldpar,Npop,gen,n);  % evaluation plus 1-elitism
fmin=[fmin,bestpar(numel(bestnewpop))];

hax=get(h1,'Children');
plot(0:Npop:(Npop*Ngen),fmin);
xlabel('Generation')
ylabel('fmin')
title('Best Values for Each Generation')
%print the most recent figure to a file
print -djpeg 'gatraj.jpg'
hold off;
%plot(0:Npop:(Npop*Ngen),fmin);

%disp('minimum obtained:');disp(bestpar(1:n));
%disp('corresponding value by f:');disp(bestpar(numel(bestnewpop)));
%disp('evaluation number:');disp(Npop*Ngen);

